package com.ark.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ark.yygh.cmn.client.DictFeignClient;
import com.ark.yygh.enums.DictEnum;
import com.ark.yygh.hosp.repository.HospitalRepository;
import com.ark.yygh.hosp.service.HospitalService;
import com.ark.yygh.model.hosp.Hospital;
import com.ark.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    //上传医院信息
    @Override
    public void save(Map<String, Object> switchMap) {
        //map转换为hospital对象
        String jsonString = JSONObject.toJSONString(switchMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);

        //判断是否存在
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        //不存在则添加
        if (hospitalExist == null) {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //存在则更新
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(hospitalExist.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    //根据hoscode查询医院
    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 医院列表
     * @param page
     * @param limit
     * @param hospitalQueryVo
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Pageable pageable = PageRequest.of(page-1,limit);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase();

        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);

        //获取查询list集合，遍历进行医院等级封装
        hospitalPage.getContent().stream().forEach(item->{
            this.packHospital(item);
        });

        return hospitalPage;
    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        if (status.intValue()==1 || status.intValue()==0){
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    //根据id获取医院详情
    @Override
    public Map<String, Object> getHospById(String id) {
        Map map = new HashMap<String,Object>();
        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());
        //医院的基本信息（包含医院等级）
        map.put("hospital",hospital);
        //单独处理更直观
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    //根据医院编号获取医院名称
    @Override
    public String getHosName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null){
            return hospital.getHosname();
        }
        return "";
    }

    //根据医院名称模糊查询
    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> list = hospitalRepository.findHospitalByHosnameLike(hosname);
        return list;
    }

    //医院预约挂号详情
    @Override
    public Map<String, Object> item(String hoscode) {
        //调用方法根据hoscode获取医院
        Hospital hospital = getHospitalByHoscode(hoscode);
        //结果集
        Map<String,Object> result = new HashMap<>();
        result.put("bookingRule",hospital.getBookingRule());
        result.put("hospital",hospital);
        hospital.setBookingRule(null);
        return result;
    }

    //医院等级封装
    private Hospital packHospital(Hospital hospital){

        //根据 dictcode 和 value 获取医院等级名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());

        //查询省市区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString + cityString + districtString + hospital.getAddress());

        return hospital;
    }

}
